/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PrivateChatView.java
 *
 * Created on 16-Feb-2011, 11:43:39
 */
package org.client;

import java.awt.event.KeyEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.StyleContext;
import javax.swing.text.Style;

/**
 *
 * @author twk
 */
public class PrivateChatView extends javax.swing.JFrame {

    /** Creates new form PrivateChatView */
    public PrivateChatView(String to, PapinhoClient client, PapinhoView view) {
        this.to = to;
        this.client = client;
        this.mainView = view;
        setTitle("Private chat with " + to);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        taInput = new javax.swing.JTextArea();
        bSend = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taOutput = new javax.swing.JTextPane();

        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(300);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        taInput.setColumns(10);
        taInput.setWrapStyleWord(true);
        taInput.setName("taInput"); // NOI18N
        taInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                taInputKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taInputKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(taInput);

        bSend.setMnemonic('S');
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.client.PapinhoApp.class).getContext().getResourceMap(PrivateChatView.class);
        bSend.setText(resourceMap.getString("bSend.text")); // NOI18N
        bSend.setEnabled(false);
        bSend.setName("bSend"); // NOI18N
        bSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bSend, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(bSend)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jSplitPane1.setBottomComponent(jPanel1);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        taOutput.setEditable(false);
        taOutput.setName("taOutput"); // NOI18N
        jScrollPane1.setViewportView(taOutput);

        jSplitPane1.setLeftComponent(jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void appendString(String str) {
        appendString(StyleContext.DEFAULT_STYLE, str);
    }

    public void appendString(String styleName, String str) {
        try {
            Style style = mainView.getTaOutput().getStyle(styleName);
            Document doc = taOutput.getDocument();
            doc.insertString(doc.getLength(), str, style);
            taOutput.setCaretPosition(doc.getLength());
        } catch (BadLocationException blex) {
            System.out.println(blex.getMessage());
        }
    }

    private void taInputKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taInputKeyReleased
        if (this.taInput.getText().length() == 0) {
            bSend.setEnabled(false);//le bouton envoyer devient grise
        } else {
            bSend.setEnabled(true);
        }
    }//GEN-LAST:event_taInputKeyReleased

    private void taInputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taInputKeyPressed
        if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_ENTER) {
            taInput.append("\n");
        } else if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            bSendActionPerformed(null);
            evt.setKeyCode(java.awt.event.KeyEvent.VK_UNDEFINED);
        }
    }//GEN-LAST:event_taInputKeyPressed

    private void bSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSendActionPerformed
        if (taInput.getText().length() > 0) {
            client.sendMessage(client.getName(), taInput.getText(), to);
            appendString("<");
            appendString(client.getName(), client.getName());
            appendString("> ");
            appendString(taInput.getText() + "\n");
            taInput.setText("");
            bSend.setEnabled(false);
        }
    }//GEN-LAST:event_bSendActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bSend;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea taInput;
    private javax.swing.JTextPane taOutput;
    // End of variables declaration//GEN-END:variables
    private String to;
    private PapinhoClient client;
    private PapinhoView mainView;
}
